package com.ibm.law.ui.system.setup;

import java.util.Iterator;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.property.Properties;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.nebula.widgets.grid.GridItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;

import com.ibm.law.domain.Category;
import com.ibm.law.domain.ChargeItem;
import com.ibm.law.service.system.CategoryService;
import com.ibm.law.service.system.ChargeItemService;
import com.ibm.law.ui.AppWindow;

import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

public class ChargeItemSetup extends Composite {
	private DataBindingContext m_bindingContext;
	private final ChargeItemTable table_1;
	private java.util.List<Category> categories;
	private ListViewer listViewer;
	private ChargeItemService chargeItemService=new ChargeItemService();
	private java.util.List<ChargeItem> chargeItems;
	private Category category;
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public ChargeItemSetup(Composite parent, int style) {
		super(parent, SWT.NONE);
		CategoryService categoryService=new  CategoryService();
		categories=categoryService.getAll();
		category=categories.get(0);
		setLayout(new GridLayout(2, false));
		listViewer = new ListViewer(this, SWT.BORDER | SWT.V_SCROLL);
		listViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent e) {
				IStructuredSelection selection=(IStructuredSelection) e.getSelection();
				Iterator<Category> iterator=selection.iterator();
				while(iterator.hasNext()){
					category=iterator.next();
					java.util.List<ChargeItem> chargeItems=chargeItemService.findByCategory(category);
					table_1.updateData(chargeItems);
					
				}
			}
		});
		List list = listViewer.getList();
		list.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		table_1=new ChargeItemTable(this, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL,categories.get(0));
		table_1.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				GridItem[] items=table_1.getSelection();
				if(items!=null){
					ChargeItemAddDialog dialog=new ChargeItemAddDialog(table_1.getShell(), SWT.DIALOG_TRIM
						| SWT.APPLICATION_MODAL);
					dialog.setChargeItem((ChargeItem)items[0].getData());
					Object result=dialog.open();
					if(result!=null){
						table_1.updateData(chargeItemService.findByCategory(category));
					}
				}
			}
		});
		table_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		new Label(this, SWT.NONE);
		new Label(this, SWT.NONE);
		
		Composite composite = new Composite(this, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		composite.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ChargeItemAddDialog dialog=new ChargeItemAddDialog(AppWindow.getInstance().getShell(), SWT.DIALOG_TRIM
						| SWT.APPLICATION_MODAL);
				ChargeItem chargeItem=new ChargeItem();
				chargeItem.setCategory(category);
				dialog.setChargeItem(chargeItem);
				Object result=dialog.open();
				if(result!=null){
					table_1.updateData(chargeItemService.findByCategory(category));
				}
			}
		});
		btnNewButton.setText("增加");
		
		Button btnNewButton_1 = new Button(composite, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				GridItem[] items=table_1.getSelection();
				for(GridItem item:items){
					chargeItemService.delete((ChargeItem)item.getData());
					
				}
				table_1.remove(table_1.getSelectionIndices());
			}
		});
		btnNewButton_1.setText("删除");
		
		Button btnNewButton_2 = new Button(composite, SWT.NONE);
		btnNewButton_2.setText("保存");
		
		Button btnNewButton_3 = new Button(composite, SWT.NONE);
		btnNewButton_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (e.getSource() instanceof Button) {
					Button button = (Button) e.getSource();
					button.getShell().close();
					
				}
			}
		});
		btnNewButton_3.setText("退出");
		new Label(this, SWT.NONE);
		m_bindingContext = initDataBindings();

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
		IObservableMap observeMap = BeansObservables.observeMap(listContentProvider.getKnownElements(), Category.class, "name");
		listViewer.setLabelProvider(new ObservableMapLabelProvider(observeMap));
		listViewer.setContentProvider(listContentProvider);
		//
		IObservableList selfList = Properties.selfList(Category.class).observe(categories);
		listViewer.setInput(selfList);
		//
		return bindingContext;
	}
}
